package com.tofindaway.daoimpl;

import java.util.List;

import com.tofindaway.dao.AbstractDaoImp;
import com.tofindaway.dao.GenericDao;

public class GenericDaoImpl<T> extends AbstractDaoImp implements GenericDao<T> {

	private Class<?> type;

	public GenericDaoImpl(Class<?> type) {
		this.type = type;
	}

	public List<T> getList() {
		@SuppressWarnings("unchecked")
		List<T> list = getSession().createCriteria(type).list();
		return list;
	}

	public T findById(Long id) {
		@SuppressWarnings("unchecked")
		T instance = (T) getSession().get(type, id);
		return instance;
	}

	public void delete(T instance) {
		getSession().delete(instance);
	}

	public T deleteById(Long id) {
		// is it useful ?
		T instance = findById(id);
		getSession().delete(instance);
		return instance;
	}

	public void update(T instance) {
		getSession().merge(instance);
	}

	public void create(T instance) {
		getSession().save(instance);
	}

}
